package tw.cating.karmasaver.alarm;

import java.util.Calendar;

import tw.cating.Utility.ADUtility;
import tw.cating.karmasaver.R;
import tw.cating.karmasaver.intef.selectedChangeListener;
import android.app.Activity;
import android.app.TimePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TimePicker;

public class alarmActivity extends Activity implements selectedChangeListener, TimePickerDialog.OnTimeSetListener{
	Context mContext =null;
	ListView mListView = null;
	Button btn_finish = null;
	Button btn_checknew = null;
	alarmAdapter mAdapter = null;
	int checkedCount = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.main);
		btn_finish = (Button)findViewById(R.id.btn_editrss);
		btn_finish.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (checkedCount != 0) {
					mAdapter.deleteCheckedItem();
				} else {
					finish();
				}
			}
		});
		
		btn_checknew = (Button)findViewById(R.id.btn_checknew);
		btn_checknew.setText(mContext.getResources().getString(R.string.addalarm));
		btn_checknew.setVisibility(View.VISIBLE);
		btn_checknew.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				addAlarmDialog();
			}
		});
		mAdapter = new alarmAdapter(this);
		mAdapter.setSelectedChangeListener(this);
		
		mListView = (ListView)findViewById(R.id.rsslist);
		mListView.setAdapter(mAdapter);
		
		setFinishButton();
		WebView adView = (WebView)findViewById(R.id.ad_view);
		ADUtility.prepareADWebView(this, adView);
		
		if (mAdapter.getCount() == 0) {
			addAlarmDialog();
		}
	}
	
	public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
		// TODO Auto-generated method stub
		alarmMeta alarm = new alarmMeta(hourOfDay, minute);
		mAdapter.addAlarm(alarm);
	}
	
	public void addAlarmDialog() {
		Calendar c = Calendar.getInstance();
		TimePickerDialog dialog = new TimePickerDialog(mContext, this, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), true);
		dialog.show();
	}
	
	public void setFinishButton() {
		if (checkedCount > 0) {
			btn_finish.setText(R.string.rss_check_delete);
		} else {
			btn_finish.setText(R.string.rss_check_finish);
		}
	}

	public void onChanged() {
		// TODO Auto-generated method stub
		checkedCount = 0;
		for (int i = 0 ; i < mAdapter.getCount(); i++) {
			alarmMeta alarm = (alarmMeta)mAdapter.getItem(i);
			if (alarm.bIsChecked)
				checkedCount++;
		}
		setFinishButton();
	}
	
    @Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();
		mAdapter.writebackSharedPreferences();
		alarmReceiver.setNextAlarm(mContext);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
	        finish();
	    }
	    return super.onKeyDown(keyCode, event);
	}
}
